<?php
declare(strict_types=1);
namespace cms\exam\service;

use cms\exam\model\RoomModel;
use cms\exam\model\TopicModel;
use Hyperf\Di\Annotation\Inject;

class RoomService extends CommonService
{
    /**
     * @Inject()
     * @var RoomModel
     */
    public RoomModel $model;

    public function createPaperContent(RoomModel $roomModel): array
    {
        $content = [];
        switch ($roomModel->make_mode)
        {
            case 1: // 手动抽题组卷
                break;
            case 2: // 题库抽题组卷
                $make_config = $roomModel->setting['make'];
                foreach ($make_config as $k => $v) {
                    foreach ($v['topic_group'] as $_k => $_v) {
                        foreach ($_v as $__k => $__v) {
                            $level = substr($__k, 6);
                            $topic_list = make(TopicModel::class)->where('type', $v['topic_type'])->where('group_id', $_k)->where('level', $level)->inRandomOrder()->take($__v['number'])->get();
                            if ($topic_list->isEmpty()) {
                                continue;
                            }
                            $topic_array = [];
                            foreach ($topic_list as $topic_k => $topic_v) {
                                $topic_array[$topic_k] = [
                                    'id' => $topic_v->id,
                                    'type' => $topic_v->type,
                                    'type_name' => $topic_v->type_name,
                                    'level' => $topic_v->level,
                                    'subject' => $topic_v->content['subject'],
                                    'answer' => $topic_v->content['answer'],
                                    'score' => $__v['score'],
                                ];
                            }
                            $content = array_merge($content, $topic_array);
                        }
                    }
                }
                break;
            default:
                break;
        }
        return $content;
    }

}